/*
 * Copyright 2019 Flipkart Internet Pvt. Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.flipkart.ohos.proteus.parser.custom;

import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Text;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.text.Font;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;
import ohos.app.AbilityContext;
import ohos.global.resource.Resource;
import ohos.global.resource.ResourceManager;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;

import com.flipkart.ohos.proteus.ProteusContext;
import com.flipkart.ohos.proteus.ProteusView;
import com.flipkart.ohos.proteus.ViewTypeParser;
import com.flipkart.ohos.proteus.parser.ParseHelper;
import com.flipkart.ohos.proteus.processor.BooleanAttributeProcessor;
import com.flipkart.ohos.proteus.processor.ColorResourceProcessor;
import com.flipkart.ohos.proteus.processor.DimensionAttributeProcessor;
import com.flipkart.ohos.proteus.processor.StringAttributeProcessor;
import com.flipkart.ohos.proteus.toolbox.Attributes;
import com.flipkart.ohos.proteus.value.Layout;
import com.flipkart.ohos.proteus.value.ObjectValue;
import com.flipkart.ohos.proteus.view.ProteusText;

import java.io.IOException;

/**
 * Created by kiran.kumar on 12/05/14.
 */
public class TextParser<T extends Text> extends ViewTypeParser<T> {
    private static final String BOTTOM = "bottom";
    private static final String CENTER = "center";
    private static final String END = "end";
    private static final String HORIZONTAL_CENTER = "horizontal_center";
    private static final String LEFT = "left";
    private static final String RIGHT = "right";
    private static final String START = "start";
    private static final String TOP = "top";
    private static final String VERTICAL_CENTER = "vertical_center";
    private static final int ZERO = 0;
    private static final int ONE = 1;
    private static final int TWO = 2;
    private static final int THREE = 3;
    private AbilityContext context;

    @Override
    public String getType() {
        return "Text";
    }

    @Override
    public String getParentType() {
        return "Component";
    }

    @Override
    public ProteusView createView(ProteusContext proteusContext, Layout layout, ObjectValue data,
                                  ComponentContainer parent, int dataIndex) {
        this.context = proteusContext.context;
        return new ProteusText(proteusContext);
    }

    @Override
    protected void addAttributeProcessors() {
        addAttributeProcessor(Attributes.Text.HTML, new StringAttributeProcessor<T>() {
            @Override
            public void setString(T view, String value) {
                view.setText(value);
            }
        });
        addAttributeProcessor(Attributes.Text.Text, new StringAttributeProcessor<T>() {
            @Override
            public void setString(T view, String value) {
                view.setText(value);
            }
        });
        addAttributeProcessor(Attributes.Text.ElementPadding, new DimensionAttributeProcessor<T>() {
            @Override
            public void setDimension(T view, float dimension) {
                view.setAroundElementsPadding((int) dimension);
            }
        });
        addAttributeProcessor(Attributes.Text.TextSize, new DimensionAttributeProcessor<T>() {
            @Override
            public void setDimension(T view, float dimension) {
                view.setTextSize((int) dimension);
            }
        });
        addAttributeProcessor(Attributes.Text.TextAlignment, new StringAttributeProcessor<T>() {
            @Override
            public void setString(T view, String value) {
                switch (value) {
                    case BOTTOM:
                        view.setTextAlignment(TextAlignment.BOTTOM);
                        break;
                    case CENTER:
                        view.setTextAlignment(TextAlignment.CENTER);
                        break;
                    case END:
                        view.setTextAlignment(TextAlignment.END);
                        break;
                    case HORIZONTAL_CENTER:
                        view.setTextAlignment(TextAlignment.HORIZONTAL_CENTER);
                        break;
                    case LEFT:
                        view.setTextAlignment(TextAlignment.LEFT);
                        break;
                    case RIGHT:
                        view.setTextAlignment(TextAlignment.RIGHT);
                        break;
                    case START:
                        view.setTextAlignment(TextAlignment.START);
                        break;
                    case TOP:
                        view.setTextAlignment(TextAlignment.TOP);
                        break;
                    case VERTICAL_CENTER:
                        view.setTextAlignment(TextAlignment.VERTICAL_CENTER);
                        break;
                    default:
                        break;
                }
            }
        });
        addAttributeProcessor(Attributes.Text.TextColor, new ColorResourceProcessor<T>() {
            @Override
            public void setColor(T view, int color) {
                view.setTextColor(new Color(color));
            }

            @Override
            public void setColor(T view, Color color) {
                view.setTextColor(color);
            }
        });
        addAttributeProcessor(Attributes.Text.TextColorHint, new ColorResourceProcessor<T>() {
            @Override
            public void setColor(T view, int color) {
                view.setHintColor(new Color(color));
            }

            @Override
            public void setColor(T view, Color color) {
            }
        });
        addAttributeProcessor(Attributes.Text.TextColorLink, new ColorResourceProcessor<T>() {
            @Override
            public void setColor(T view, int color) {
                //view.setLinkTextColor(new Color(color));
            }

            @Override
            public void setColor(T view, Color color) {
            }
        });

        addAttributeProcessor(Attributes.Text.TextColorHighLight, new ColorResourceProcessor<T>() {
            @Override
            public void setColor(T view, int color) {
                view.setHintColor(new Color(color));
            }

            @Override
            public void setColor(T view, Color color) {
            }
        });

        addAttributeProcessor(Attributes.Text.ElementLeft, new StringAttributeProcessor<T>() {
            @Override
            public void setString(T view, String value) {
                Element[] compoundElements = view.getAroundElements();
                ResourceManager resourceManager = context.getResourceManager();
                Resource resource;
                try {
                    if (resourceManager != null) {
                        resource = resourceManager.getRawFileEntry(value).openRawFile();
                        ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
                        decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
                        ImageSource imageSource = ImageSource.create(resource, new ImageSource.SourceOptions());
                        PixelMap pixelMap = imageSource.createPixelmap(decodingOptions);
                        view.setAroundElements(new PixelMapElement(pixelMap), compoundElements[ONE],
                            compoundElements[TWO], compoundElements[THREE]);
                        pixelMap.release();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        addAttributeProcessor(Attributes.Text.ElementTop, new StringAttributeProcessor<T>() {
            @Override
            public void setString(T view, String value) {
                Element[] compoundElements = view.getAroundElements();
                ResourceManager resourceManager = context.getResourceManager();
                Resource resource;
                try {
                    if (resourceManager != null) {
                        resource = resourceManager.getRawFileEntry(value).openRawFile();
                        ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
                        decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
                        ImageSource imageSource = ImageSource.create(resource, new ImageSource.SourceOptions());
                        PixelMap pixelMap = imageSource.createPixelmap(decodingOptions);
                        view.setAroundElements(compoundElements[ZERO], new PixelMapElement(pixelMap),
                            compoundElements[TWO], compoundElements[THREE]);
                        pixelMap.release();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        addAttributeProcessor(Attributes.Text.ElementRight, new StringAttributeProcessor<T>() {
            @Override
            public void setString(T view, String value) {
                Element[] compoundElements = view.getAroundElements();
                ResourceManager resourceManager = context.getResourceManager();
                Resource resource;
                try {
                    if (resourceManager != null) {
                        resource = resourceManager.getRawFileEntry(value).openRawFile();
                        ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
                        decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
                        ImageSource imageSource = ImageSource.create(resource, new ImageSource.SourceOptions());
                        PixelMap pixelMap = imageSource.createPixelmap(decodingOptions);
                        view.setAroundElements(compoundElements[ZERO], compoundElements[ONE],
                            new PixelMapElement(pixelMap), compoundElements[THREE]);
                        pixelMap.release();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        addAttributeProcessor(Attributes.Text.ElementBottom, new StringAttributeProcessor<T>() {
            @Override
            public void setString(T view, String value) {
                Element[] compoundElements = view.getAroundElements();
                ResourceManager resourceManager = context.getResourceManager();
                Resource resource;
                try {
                    if (resourceManager != null) {
                        resource = resourceManager.getRawFileEntry(value).openRawFile();
                        ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
                        decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
                        ImageSource imageSource = ImageSource.create(resource, new ImageSource.SourceOptions());
                        PixelMap pixelMap = imageSource.createPixelmap(decodingOptions);
                        view.setAroundElements(compoundElements[ZERO], compoundElements[ONE],
                            compoundElements[TWO], new PixelMapElement(pixelMap));
                        pixelMap.release();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

        addAttributeProcessor(Attributes.Text.MaxLines, new StringAttributeProcessor<T>() {
            @Override
            public void setString(T view, String value) {
                view.setMaxTextLines(ParseHelper.parseInt(value));
            }
        });

        addAttributeProcessor(Attributes.Text.Ellipsize, new StringAttributeProcessor<T>() {
            @Override
            public void setString(T view, String value) {
                Enum ellipsize = ParseHelper.parseEllipsize(value);
                view.setTruncationMode((Text.TruncationMode) ellipsize);
            }
        });

        addAttributeProcessor(Attributes.Text.PaintFlags, new StringAttributeProcessor<T>() {
            @Override
            public void setString(T view, String value) {
                //if (value.equals("strike")) {
                //view.setPaintFlags(view.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
                //}
            }
        });

        addAttributeProcessor(Attributes.Text.Prefix, new StringAttributeProcessor<T>() {
            @Override
            public void setString(T view, String value) {
                view.setText(value + view.getText());
            }
        });

        addAttributeProcessor(Attributes.Text.Suffix, new StringAttributeProcessor<T>() {
            @Override
            public void setString(T view, String value) {
                view.setText(view.getText() + value);
            }
        });

        addAttributeProcessor(Attributes.Text.TextStyle, new StringAttributeProcessor<T>() {
            @Override
            public void setString(T view, String value) {
                int typeface = ParseHelper.parseTextStyle(value);
                view.setFont(Font.DEFAULT);
                //view.setTypeface(Typeface.defaultFromStyle(typeface));
            }
        });

        addAttributeProcessor(Attributes.Text.SingleLine, new BooleanAttributeProcessor<T>() {
            @Override
            public void setBoolean(T view, boolean isMultiple) {
                view.setMultipleLine(!isMultiple);
            }
        });

        addAttributeProcessor(Attributes.Text.TextAllCaps, new BooleanAttributeProcessor<T>() {
            @Override
            public void setBoolean(T view, boolean value) {
                //view.setAllCaps(value);
            }
        });
        addAttributeProcessor(Attributes.Text.Hint, new StringAttributeProcessor<T>() {
            @Override
            public void setString(T view, String value) {
                view.setHint(value);
            }
        });
    }
}
